package com.jp.knowledge.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;


public class IPUtil {

	private static String localIPAddress;
	static {
		Enumeration allNetInterfaces = null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		InetAddress ip = null;
		while (allNetInterfaces.hasMoreElements()) {
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
			Enumeration addresses = netInterface.getInetAddresses();
			while (addresses.hasMoreElements()) {
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address) {
					if (!"127.0.0.1".equals(ip.getHostAddress().trim())) {
						localIPAddress = ip.getHostAddress();
					}
				}
			}
		}
	}

	public static String getLocalIPAddress() {
		return localIPAddress;
	}
	


	public static void main(String[] args) {
		// System.out.println(getLocalIPAddress());
		// List<TwoTuple<String, Integer>> map = new ArrayList<>();
		// map.add(new TwoTuple("174.140.169.152", 8080));
		// map.add(new TwoTuple("52.178.210.66", 3128));
		// map.add(new TwoTuple("46.105.152.87", 8898));
		// map.add(new TwoTuple("85.88.8.110", 6588));
		// map.add(new TwoTuple("59.37.199.234", 9000));
		// for (TwoTuple<String, Integer> tt : map) {
		// System.out.println(tt.getKey()+"--"+tt.getValue());
		// }
	}

}
